package br.com.andreikeda.utility.gestor.finance.storage.database.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import br.com.andreikeda.utility.gestor.finance.storage.database.model.Category;
import br.com.andreikeda.utility.gestor.finance.storage.database.model.Company;
import br.com.andreikeda.utility.gestor.finance.utils.ColumnConstants;

/**
 * Created by LimaoGames on 22/01/2015.
 * contact me: andreikeda87@gmail.com
 */
public class CompanyDAO extends GenericDAO {

    private final String TABLE = "COMPANY";
    private final String WHERE_CLAUSE_DEFAULT = "id=?";
    private final String WHERE_CLAUSE_COMPANY_ID = "company_id=?";

    private static CompanyDAO instance;

    public static CompanyDAO getInstance() {
        if (instance == null)
            instance = new CompanyDAO();
        return instance;
    }

    @Override
    public boolean delete(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Company))
            return false;
        return mLite.delete(TABLE, WHERE_CLAUSE_DEFAULT, new String[]{String.valueOf(((Company) object).getId())}) > 0;
    }

    @Override
    public void deleteAll(SQLiteDatabase mLite, String whereClause, String[] whereArgs) {
        mLite.delete(TABLE, whereClause, whereArgs);
    }

    @Override
    public boolean insert(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Company))
            return false;
        ContentValues cv = new ContentValues();
        cv.put(ColumnConstants.NAME, ((Company) object).getName());
        cv.put(ColumnConstants.CATEGORY_ID, ((Company) object).hasCategory() ? ((Company) object).getCategory().getId() : 0);
        mLite.insert(TABLE, null, cv);
        return true;
    }

    @Override
    public List<Object> list(SQLiteDatabase mLite) {
        Cursor c = mLite.query(TABLE, null, null, null, null, null, null);
        List companies = new ArrayList<Company>();
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                companies.add(
                        new Company(c.getInt(c.getColumnIndex(ColumnConstants.ID)),
                                c.getString(c.getColumnIndex(ColumnConstants.NAME)),
                                (Category) CategoryDAO.getInstance().listFrom(mLite, WHERE_CLAUSE_DEFAULT, new String[]{String.valueOf(
                                        c.getInt(c.getColumnIndex(ColumnConstants.CATEGORY_ID))
                                )}).get(0),
                                StockDAO.getInstance().listFrom(mLite, WHERE_CLAUSE_COMPANY_ID, new String[]{String.valueOf(
                                        c.getInt(c.getColumnIndex(ColumnConstants.COMPANY_ID))
                                )})));
                c.moveToNext();
            }
        }
        return companies;
    }

    @Override
    public List<Object> listFrom(SQLiteDatabase mLite, String whereClause, String[] whereArgs) {
        Cursor c = mLite.query(TABLE, null, whereClause, whereArgs, null, null, null);
        List companies = new ArrayList<Company>();
        if (c.moveToFirst()) {
            while (!c.isAfterLast()) {
                companies.add(
                        new Company(c.getInt(c.getColumnIndex(ColumnConstants.ID)),
                                c.getString(c.getColumnIndex(ColumnConstants.NAME)),
                                (Category) CategoryDAO.getInstance().listFrom(mLite, WHERE_CLAUSE_DEFAULT, new String[]{String.valueOf(
                                        c.getInt(c.getColumnIndex(ColumnConstants.CATEGORY_ID))
                                )}).get(0),
                                StockDAO.getInstance().listFrom(mLite, WHERE_CLAUSE_COMPANY_ID, new String[]{String.valueOf(
                                        c.getInt(c.getColumnIndex(ColumnConstants.COMPANY_ID))
                                )})));
                c.moveToNext();
            }
        }
        return companies;
    }

    @Override
    public boolean update(SQLiteDatabase mLite, Object object) {
        if (!(object instanceof Company))
            return false;
        ContentValues cv = new ContentValues();
        cv.put(ColumnConstants.NAME, ((Company) object).getName());
        cv.put(ColumnConstants.CATEGORY_ID, ((Company) object).hasCategory() ? ((Company) object).getCategory().getId() : 0);
        mLite.update(TABLE, cv, WHERE_CLAUSE_DEFAULT, new String[]{String.valueOf(((Company) object).getId())});
        return true;
    }

    @Override
    public void updateAll(SQLiteDatabase mLite, ContentValues cv, String whereClause, String[] whereArgs) {
        mLite.update(TABLE, cv, whereClause, whereArgs);
    }

    private CompanyDAO() {

    }
}
